package main

import (
	_ "peilian-api/bootstrap"

	"context"
	"encoding/json"
	"fmt"
	"math/rand"
	"peilian-api/app/global/common"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/log"
	"peilian-api/app/global/variable"
	"peilian-api/app/model"
	"peilian-api/app/services/report_service"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"github.com/tealeg/xlsx"
	"gorm.io/gorm"
)

func OpenFile(filepath string) ([][][]string, error) {
	return xlsx.FileToSlice(filepath)
}

var careerMap = map[string]int{
	"通用能力":        1,
	"公务员":         2,
	"互联网运营":       3,
	"产品经理":        4,
	"项目经理":        5,
	"销售":          6,
	"人力资源":        7,
	"行政":          9,
	"财务":          10,
	"游戏策划":        11,
	"银行管培生":       12,
	"市场营销":        13,
	"公共能力(自我介绍等)": 14,
	"经验类开放问题":     15,
}

var careerClerkMap = map[string]string{
	"通用能力":  "HR必问",
	"公务员":   "历年真题",
	"互联网运营": "大厂真题",
	"产品经理":  "大厂真题",
	"项目经理":  "大厂真题",
	"销售":    "500强真题",
	"人力资源":  "500强真题",
	"行政":    "500强真题",
	"财务":    "500强真题",
	"游戏策划":  "大厂真题",
	"银行管培生": "校招真题",
	"市场营销":  "500强真题",
}

func opt(filepath string) {
	// 获取在线文件
	out, err := OpenFile(filepath)
	if err != nil {
		fmt.Println("文件打开error")
		return
	}
	// 定义出错行数
	var rows []int = make([]int, 0)

	for k, v := range out[0] {
		if k == 0 {
			continue
		}
		cid, ok := careerMap[v[2]]
		if !ok {
			rows = append(rows, k)
			continue
		}
		if len(v[0]) == 0 || len(v[1]) == 0 || len(v[4]) == 0 {
			rows = append(rows, k)
			continue
		}
		var questionText = v[4]
		var question tables.Question
		d := variable.DB.MySQL.Model(&question).Where("career_id = ? and text = ?", cid, questionText).Find(&question)
		if d.Error == gorm.ErrRecordNotFound {
			rows = append(rows, k)
			fmt.Println(k)
			continue
		}
		if question.ID == 0 {
			fmt.Println(k)
		}

		// 添加数据
		variable.DB.MySQL.Create(&tables.SpecialQuestion{QuestionID: int64(question.ID), FrontShowTag: v[0], Criteria: v[1], CareerID: int64(cid), Level: question.Level})
	}
	fmt.Println("出错数据:", rows)
}

type Info struct {
	Criteria string `json:"criteria"`
	Describe string `json:"describe"`
	Num      string `json:"num"`
	Tag      string `json:"tag"`
	IsEnable bool   `json:"is_enable"`
}

func tag(filepath string) {
	file, _ := xlsx.OpenFile(filepath)
	sheet := file.Sheets[1]

	for i := 0; i < 18; i++ {
		var out_base []Info
		var out_adv []Info
		tag := careerClerkMap[sheet.Cell(1, i).Value]
		for j := 3; j < 30; j++ {
			var o = Info{Tag: tag, Describe: "%s人次练过", IsEnable: true}
			c := sheet.Cell(j, i)
			if len(c.Value) == 0 {
				continue
			}
			o.Criteria = c.Value
			o.Num = strconv.Itoa(int(rand.Int31n(1000) + 2800))
			out_base = append(out_base, o)
			o.Num = strconv.Itoa(int(rand.Int31n(1000) + 3000))
			out_adv = append(out_adv, o)
		}
		if len(out_base) == 0 {
			continue
		}
		base, _ := json.Marshal(out_base)
		adv, _ := json.Marshal(out_adv)
		// 更新数据
		variable.DB.MySQL.Model(&tables.Career{}).Where("name = ?", sheet.Cell(1, i).Value).Updates(map[string]interface{}{"special_advance": string(adv), "special_base": base})

	}

}

func main() {
	// var filepath = "/Users/a123/Desktop/专项练习题库v1.5.1.xlsx"
	// opt(filepath)
	// tag(filepath)
	c := &gin.Context{}
	uuidStr := strings.ReplaceAll(uuid.NewString(), "-", "")
	c.Set(common.TraceKey, context.WithValue(context.Background(), common.TraceKey, &log.Trace{TraceId: uuidStr, SpanId: uuidStr, Caller: ""}))

	var special tables.SpecialRecord
	variable.DB.MySQL.Model(&tables.SpecialRecord{}).Where("id = 22168").Find(&special)

	jobName := special.CareerName
	ansText := special.AnswerText
	ansAnalysisTag := special.AnsAnalysisTag

	partCacheListStr := report_service.FetchLabelsCache(c, jobName, ansText)
	labelCacheList := make([]interface{}, 0)
	var partLabelCache interface{}
	_ = json.Unmarshal([]byte(partCacheListStr), &partLabelCache)
	labelCacheList = append(labelCacheList, partLabelCache)

	var specialInfoAnsAnalysis = make([]interface{}, 0)
	_ = json.Unmarshal([]byte(ansAnalysisTag), &specialInfoAnsAnalysis)

	respStr := report_service.FetchEvalByLabelsZhuanxiang(c, jobName, []interface{}{partLabelCache}, specialInfoAnsAnalysis, []interface{}{special.CriteriaTag})
	var ebzResp model.EvalByLabelsZhuanxiangResp
	_ = json.Unmarshal([]byte(respStr), &ebzResp)
	score, _ := strconv.Atoi(fmt.Sprintf("%1.0f", ebzResp.RawScore*100))

	// s2 := report_service.FetchEvalByLabelsZhuanxiang(c, jobName, labelCacheList, analysisTag, testTag)
	fmt.Println(score)

	// report_service.FetchLabelsCache()
}
